package org.cid54.jshopper.controller.csv;

import java.util.List;
import org.cid54.jshopper.controller.csv.util.GuidChecker;
import org.cid54.jshopper.model.User;
import org.cid54.jshopper.model.UserGroup;
import org.cid54.jshopper.service.UserAndGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author fgo
 */
@Controller
@RequestMapping("csv/{userGuid}/usergroups")
public class CsvUserGroupController {

    @Autowired
    private GuidChecker guidChecker;
    @Autowired
    private UserAndGroupService groupService;

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public String doGetAll(@PathVariable("userGuid") String userGuid) {

        if (this.guidChecker.checkGuid(userGuid)) {

            User user = this.groupService.getUserByMobileGuid(userGuid);

            List<UserGroup> userGroups = this.groupService.getUserGroupsByUserId(user.getId());

            StringBuilder sb = new StringBuilder();

            for (UserGroup userGroup : userGroups) {

                sb.append(userGroup.getName()).append("\n");

            }

            return sb.toString();

        } else {
            return "";
        }

    }
}
